package top.yuyufeng.mbuy.web;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.yuyufeng.mbuy.dto.CartDTO;
import top.yuyufeng.mbuy.dto.ResponseJson;
import top.yuyufeng.mbuy.dto.ShippingDTO;
import top.yuyufeng.mbuy.enums.CartCheckedEnum;
import top.yuyufeng.mbuy.query.GetPostageQuery;
import top.yuyufeng.mbuy.service.ICartService;
import top.yuyufeng.mbuy.service.IShippingService;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author yyf19
 */
@Controller
@RequestMapping(value = "user/{userId}/shipping")
public class ShippingController {
    @Autowired
    private IShippingService shippingService;

    /**
     * 获取个人收货地址
     * @param userId
     * @return
     */
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public @ResponseBody
    ResponseJson<List<ShippingDTO>> list(@PathVariable("userId") Integer userId) {
        List<ShippingDTO> shippingDTOList = shippingService.listByUserId(userId);
        return ResponseJson.success(shippingDTOList);
    }

    /**
     * 获取运费
     * @param userId
     * @param getPostageQuery
     * @return
     */
    @RequestMapping(value = "getPostage", method = RequestMethod.POST)
    public @ResponseBody
    ResponseJson<BigDecimal> getPostage(@PathVariable("userId") Integer userId, @RequestBody GetPostageQuery getPostageQuery) {
        System.out.println(JSONObject.toJSONString(getPostageQuery));
        BigDecimal result = shippingService.calPostage(getPostageQuery);
        return ResponseJson.success(result);
    }

}
